 <?php
//公共方法
function pub($config)
{
    @mysql_connect($config['host'], $config['user'], $config['pass']);
    if (mysql_errno()) {
        die(mysql_error());
    }
    mysql_select_db($config['db']);
    mysql_set_charset($config['char']);
}
//查询的函数
function sel($config, $table, $where = '')
{
    pub($config);
    $sql = "select * from {$table} {$where}";

    $res = mysql_query($sql);
    $return =[];

    while ($row = mysql_fetch_assoc($res)) {
        $return[] = $row;
    }
    return $return;
}
 
function sell($config, $where = '', $table)
{
    pub($config);
    $sql = "select distinct {$where} from {$table} ";
    $res = mysql_query($sql);
    $return =[];
   
    while ($row = mysql_fetch_assoc($res)) {
        $return[] = $row;
    }
    return $return;}


function update($config, $table, $post)
{
    pub($config);
    $str = '';
    foreach ($post as $k => $v) {
        if ($k != 'id') {
            $str .= '`' . $k . '`' . '=' . "'" . $v . "',";
        }
    }
    $sql = "update {$table} set " . rtrim($str, ',') . " where id=" . $post['id'];
    $res = mysql_query($sql);
  return $res;
}

function del($config, $table, $where)
{
    pub($config);
    $sql = "delete from {$table} {$where}";
    $res = mysql_query($sql);
    return $res;
}

function add($config, $table, $post)
{
    pub($config);
    $fields = implode('`,`', array_keys($post));
    $values = implode("','", array_values($post));
    $sql = "insert into {$table}(`" . $fields . "`) values('" . $values . "')";
    $res = mysql_query($sql);
    
    return $res;
    
}

function up($name, $dir = '/public/upload/', $max_size =2100000, $allow_type = array('jpg', 'png', 'gif'))
{
    $file = $_FILES[$name];
    $ext  = trim(strchr($file['name'], '.'), '.');
    //判断错误号
    // var_dump($file['error']);die;
if ($file['error'] != 0) {
    if ($file['error'] = 4) {
        return;
    }elseif (condition) {
      echo '上传错误: ';
        switch ($_FILES['tu']['error']) {
            case 1:die('上传文件大小超出了PHP配置中的约定值：upload_max_filesize');
            case 2:die('上传文件大小超出了表单中的约定值：MAX_FILE_SIZE');
            case 3:die('文件只被部分上载');
            // case 4:die('没有上传任何文件');
            case 6:die('找不到临时文件夹');
            case 7:die('文件写入失败');
            default:die('末知错误');
        }
    }
}
    //判断上传的文件大小
    if ($file['size'] > $max_size) {
        die('上传文件太大了我受不了！');
    }
    //判断上传的文件格式
    if (!in_array($ext, $allow_type)) {
        die('你当我瞎啊，请传正确的格式！');
    }
    //随机文件名
    $filename = date('YmdHis') . mt_rand(100, 999) . '.' . $ext;
    // echo $filename;die;
    if (is_uploaded_file($file['tmp_name'])) {
        // if (!file_exists($dir)) {
        //     $res = mkdir($dir);
        // }
   if (move_uploaded_file($_FILES[$name]['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.trim($dir, '/') . '/' . $filename)) {
            return '/'.(trim($dir, '/') . '/' . $filename);
        } else {
            echo '移动失败';
        }
    } else {
        die('非法上传');
    }
}




// function page($config,$table,$num =6,$lr=1){
// pub($config);
// $p =isset($_GET['p'])?$_GET['p']:'1';

// $offset = ($p-1)*$num;
// // $sql = "select * from $table limit $offset,$num";
// $sql2 = "select count(*) from $table ";
// // $res =mysql_query($sql);
// $res2 =mysql_query($sql2);
// $row2 = mysql_fetch_assoc($res2);
// $ss= $row2['count(*)'];
// $ye = ceil($ss/$num);

//     if(!(($p>=1)&&($p<=$ye))){
        
//         echo '<script>alert("输入的页码错误")</script>';
//         echo '<script>window.location="'.$_SERVER['PHP_SELF'].'?p=1"</script>';
//     }

// $prev =$p-1;
// if($prev<1){
//     $prev=1;
// }
// $next =$p+1;
// if($next>$ye){
//   $next=$ye;
// }

// $left=$p-$lr;
// $right =$p+$lr;
// if($right>=$ye){
//     $right=$ye;
//     $left=$ye-(2*$lr);
// }
// if($left<=0){
//     $left=1;
//     $right=2*$lr+1;
//     if($right>$ye){
//         $right=$ye;
//     }
// }
// $str = '';
// $str.= "<a href='{$_SERVER['PHP_SELF']}?p=1' >首页</a>";
// $str.= "<a href='{$_SERVER['PHP_SELF']}?p=$prev' >上一页</a>";
// if($left!=1){
// $str.= "...";
// }


// for($i=$left;$i<=$right;$i++){
    
//     if($i==$p){
//         $color = "red";
//     }else{$color = '';}
//     $str.= "&nbsp;<a href='{$_SERVER['PHP_SELF']}?p={$i}' style='color:{$color}' >{$i}</a>&nbsp;";
// }
// if($right!=$ye){
// $str.= "...";}
// $str.= "<a href='{$_SERVER['PHP_SELF']}?p=$next' >下一页</a>";
// $str.= "<a href='{$_SERVER['PHP_SELF']}?p={$ye}' >尾页</a>";
// // var_dump($_SERVER['PHP_SELF']);
// $str.="总共{$ye}页，当前{$p}/{$ye}页";
// return $str;
// }
function page($config,$table,$aaa=1){
 pub($config);

$p=isset($_GET["p"])?$_GET["p"]:1;
$num = 5;
$page=($p-1)*3;



$sql1="select count(*) as c from $table ";

$a1=mysql_query($sql1);
$b1=mysql_fetch_assoc($a1);
$count=$b1['c'];

$prv=$p-1;
if($prv<=0){
    $prv=1;
}
$next=$p+1;
$y=ceil($count/3);
if($next>ceil($count/3)){
    $next=ceil($count/3);
}

$left=$p-$aaa;
if($left<=0){
    $left=1;
}

$right=$p+$aaa;
if($right>$y){
    $right=$y; 
}
  if($left==1){
     $right=$aaa*2+1;
     if($right>$y)
     {
        $right=$y;
     } 
 }
 if($right==$y){
      $left=$y-$aaa*2;
      if($left<=0)
      {
        $left=1;
      }
  }
$str="";
 $str.='<a href="'.$_SERVER['PHP_SELF'].'?p=1">首页</a>';
$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p='.$prv.'">上一页</a>';
  if($left!=1){
    $str.= "...";
  }
 
    for($i=$left;$i<=$right;$i++){
  $str.=     
  '<a href="'.$_SERVER['PHP_SELF'].'?p='.$i.'">'.$i.'</a>';
}
  if($right!=$y){
   $str.= "...";
  }
$str.=    '<a href="'.$_SERVER['PHP_SELF'].'?p='.$next.'">下一页</a>';
 
$str.= '<a href="'.$_SERVER['PHP_SELF'].'?p='.ceil($count/$num).'">尾页</a>';
$str.='当前：'.$p.'/'.ceil($count/$num).'<br>';
return $str;
}